﻿Ext.define('app.prm.project.PrmApply', {
    extend: 'Ext.panel.Panel',
    controller: 'prmApply',
    requires: ['app.prm.project.PrmApplyController', 'app.prm.project.PrmApplyModel'], viewModel: {
        type: 'PrmApplyModel'
    },
    border: false,
    layout: 'form',
    autoScroll: true,
    title: '',
    userIds: '',
    accounts: '',
    proExist: false,
    tbar: [{ text: '提交', iconCls: 'accept', handler: 'submitApply' }],
    initComponent: function () {
        this.items = [this.getForm(), this.getHistory()];
        this.callParent();
    },
    getForm: function () {
        var main = this;
        if (!this.form) {
            this.form = new Ext.form.FormPanel(
            {
                border: false,
                frame: false,
                labelAlign: 'right',
                bodyStyle: 'padding:5px',
                items: [
                    {
                        layout: 'column',
                        border: false,
                        items: [
                            {
                                border: false,
                                columnWidth: 0.3,
                                layout: 'form',
                                items: [
                                    {
                                        xtype: 'textfield',
                                        width: 150,
                                        bind: { value: '{projectNo}' },
                                        allowBlank: false,
                                        name: 'ProjectNo',
                                        fieldLabel: '项目编号',
                                        listeners: {
                                            blur: 'setAddress'
                                        }
                                    },
                                    {
                                        xtype: 'datefield',
                                        width: 150,
                                        format: 'Y-m-d',
                                        allowBlank: false,
                                        name: 'EffectiveTime',
                                        bind: { value: '{effectiveTime}' },
                                        fieldLabel: '生效时间'
                                    }
                                ]
                            },
                            {
                                border: false,
                                columnWidth: 0.3,
                                layout: 'form',
                                items: [
                                    this.getAddress(),
                                    {
                                        xtype: 'datefield',
                                        width: 170,
                                        format: 'Y-m-d',
                                        bind: { value: '{expirationTime}' },
                                        allowBlank: false,
                                        name: 'ExpirationTime',
                                        fieldLabel: '失效时间'
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        fieldLabel: '申请说明',
                        height: 330,
                        name: 'Note',
                        bind: { value: '{note}' },
                        width: 840,
                        xtype: 'textarea'
                    }
                ]
            });
        }
        return this.form;
    },
    getAddress: function () {
        if (!this.address) {
            this.address = new Ext.form.DisplayField(
            {
                xtype: 'displayfield',
                width: 170,
                fieldLabel: '审批人',
                bind: { value: '{auditor}' }
            });
        }
        return this.address;
    },
    getHistory: function () {
        var main = this;
        if (!this.history) {
            this.history = new Ext.tab.Panel(
            {
                border: false,
                title: '申请历史',
                height: 400,
                style: 'margin: 5px 0 0 0',
                activeTab: 0,
                layout: 'hbox',
                items: [
                    {
                        border: false,
                        title: '所有',
                        name: 'all', layout: 'fit',
                        items: [this.getGrid("所有")]
                    },
                    {
                        border: false,
                        name: 'NotReviewed',
                        title: '未审核', layout: 'fit',
                        items: []
                    },
                    {
                        border: false,
                        name: 'ReviewBy',
                        title: '审核通过', layout: 'fit',
                        items: []
                    },
                    {
                        border: false,
                        name: 'AuditDismissed',
                        title: '审核驳回', layout: 'fit',
                        items: []
                    }
                ],
                listeners:
                {
                    tabchange: function (sender, tab, oldcard) {
                        var tabName = tab.name;
                        tab.removeAll(true);
                        if (tabName == "all") {
                            var grid = main.getGrid("所有");
                            tab.items.add(grid);
                        }
                        if (tabName == "NotReviewed") {
                            var grid = main.getGrid("未审核");
                            tab.items.add(grid);
                        }
                        if (tabName == "ReviewBy") {
                            var grid = main.getGrid("通过");
                            tab.items.add(grid);
                        }
                        if (tabName == "AuditDismissed") {
                            var grid = main.getGrid("驳回");
                            tab.items.add(grid);
                        }
                        sender.updateLayout();
                    }
                }
            });
        }
        return this.history;
    },
    getGrid: function (status) {



        var expander = new Ext.grid.plugin.RowExpander({
            rowBodyTpl: '<p>申请说明：{appNote}' +
            '<br>审批状态：{appStatusTxt}' +
             '<br>审批人员：{checkUsers}' +
            '<br>审核说明：{checkNote}'
        });

        return new Ext.grid.Panel(
            {
                border: false,
                forceFit: true,
                store: new Ext.data.JsonStore(
                {
                    proxy: {
                        type: 'ajax',
                        reader: {
                            type: 'json',
                            rootProperty: function (data) {
                                // Extract child nodes from the items or children property in the dataset
                                return data.result || data.children;
                            }
                        },
                        url: '/api/services/app/projectAp/GetApplysForMe?status=' + status,

                    },
                    autoLoad: true,
                    fields: ['projectNo', { name: 'effectiveTime', type: 'date', dateFormat: 'c' },
                        { name: 'expirationTime', type: 'date', dateFormat: 'c' },
                        { name: 'createOn', type: 'date', dateFormat: 'c' },
                        { name: 'checkOn', type: 'date', dateFormat: 'c' },
                         'appNote', 'appStatusTxt', 'checkNote', 'checkUsers'],
                    listeners: {
                        'load': function (store, records) {
                            console.log('loading data');
                            console.log(records);
                        }
                    }
                }),
                plugins: expander,
                columns: [
                new Ext.grid.RowNumberer(),
                    { header: "项目编号", dataIndex: 'projectNo' },
                    { header: "生效时间", dataIndex: 'effectiveTime', maxWidth: 150, renderer: Ext.util.Format.dateRenderer('Y-m-d')},
                    { header: "过期时间", dataIndex: 'expirationTime', maxWidth: 150, renderer: Ext.util.Format.dateRenderer('Y-m-d') },
                    { header: "提交时间", dataIndex: 'createOn', maxWidth: 150, renderer: Ext.util.Format.dateRenderer('Y-m-d') },
                    expander
                ]
            });
    }
});